package it.kion.util.ui.umenumanager2.adapter;

import it.kion.util.ui.umenumanager2.IImageKeys;
import it.kion.util.ui.umenumanager2.UMenuManager;
import it.kion.util.ui.umenumanager2.model.Folder;
import it.kion.util.ui.umenumanager2.model.Item;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IAdapterFactory;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.model.IWorkbenchAdapter;
import org.eclipse.ui.plugin.AbstractUIPlugin;

public class MenuItemsAdapterFactory implements IAdaptable, IAdapterFactory {

	private IWorkbenchAdapter folderAdapter = new IWorkbenchAdapter() {

		public Folder getParent(Object o) {
			return ((Folder) o).getParent();
		}

		public String getLabel(Object o) {
			Folder folderName = ((Folder) o);
			return folderName.getLabel();
		}

		public ImageDescriptor getImageDescriptor(Object object) {
			return AbstractUIPlugin.imageDescriptorFromPlugin(UMenuManager.PLUGIN_ID, IImageKeys.FOLDER);
		}

		public Object[] getChildren(Object o) {
			return ((Folder) o).getChildren().toArray();
		}

	};

	private IWorkbenchAdapter linkAdapter = new IWorkbenchAdapter() {

		public Folder getParent(Object o) {
			return ((Item) o).getParent();
		}

		public String getLabel(Object o) {
			Item entry = ((Item) o);
			return entry.getLabel();
		}

		public ImageDescriptor getImageDescriptor(Object object) {
			return AbstractUIPlugin.imageDescriptorFromPlugin(UMenuManager.PLUGIN_ID, IImageKeys.ITEM);
		}

		public Object[] getChildren(Object o) {
			return new Object[0];
		}
	};

	@SuppressWarnings("unchecked")
	public Object getAdapter(Object adaptableObject, Class adapterType) {
		if (adapterType == IWorkbenchAdapter.class && adaptableObject instanceof Folder) {
			return folderAdapter;
		}
		else
			if (adapterType == IWorkbenchAdapter.class && adaptableObject instanceof Item) {

				return linkAdapter;
			}
			else {
				return null;
			}
	}

	@SuppressWarnings("unchecked")
	public Class[] getAdapterList() {
		return new Class[] { IWorkbenchAdapter.class };
	}

	@SuppressWarnings("unchecked")
	public Object getAdapter(Class adapterType) {
		return null;
	}
}
